package org.roger.analyzer;

import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.en.PorterStemFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.util.CharArraySet;

/**
 * 词干停用词分析器
 * @author luoweiyi
 */
public class PorterStopAnalyzer extends Analyzer{
	
	private CharArraySet stopWords;
	
	public PorterStopAnalyzer(CharArraySet stopWords){
		this.stopWords = stopWords;
	}

	@Override
	protected TokenStreamComponents createComponents(String fieldName,
			Reader reader) {
		
		final StandardTokenizer src = new StandardTokenizer(reader);
		
		TokenStream tok = new LowerCaseFilter(src);
		tok = new StopFilter(tok,stopWords);
		tok = new PorterStemFilter(tok);
		
		return new TokenStreamComponents(src, tok) {
		      @Override
		      protected void setReader(final Reader reader) throws IOException {
		        super.setReader(reader);
		      }
		};
	}

}
